package com.vito.oa.act.extend.service.impl;

 
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.act.model.ActExTableColsModel;
import com.vito.base.model.DataGrid;
import com.vito.base.util.Contants;
import com.vito.oa.act.extend.dao.OaActExTableColsMapper;
import com.vito.oa.act.extend.dao.OaActExTableMapper;
import com.vito.oa.act.extend.model.OaActExTableColsModel;
import com.vito.oa.act.extend.service.OaActExTableColsService;


@Service(group="OaActExTableColsServiceImpl")
public class OaActExTableColsServiceImpl implements OaActExTableColsService {
	@Autowired
	private OaActExTableColsMapper actExTableColsMapper;
	@Autowired
	private OaActExTableMapper actExTableMapper;
	
	@Override
	public DataGrid<OaActExTableColsModel> queryOaActExTableColsByPage(
			OaActExTableColsModel actExTableModel) {
		DataGrid<OaActExTableColsModel> datagrid = new DataGrid<OaActExTableColsModel>();
		int count = actExTableColsMapper.queryActExTableColsCount(actExTableModel);
		datagrid.setTotal(count);
		if(count>0){
			datagrid.setRows(actExTableColsMapper.queryActExTableCols(actExTableModel));
		}
		return datagrid;
	}
	@Override
	public int saveActExTableCols(OaActExTableColsModel actExTableModel) {
		//是外键，更新从表ID信息到主表
		if(Contants.BASE_STATE.equals(actExTableModel.getIsFk())){
			String mainTableKey = actExTableModel.getFkTableKey();
			String childTableKey = actExTableModel.getTableKey();
			//String mainShortName,String childShortName
			Map<String, String> map = new HashMap<String, String>();
			map.put("mainShortName", mainTableKey);
			map.put("childShortName", childTableKey);
			actExTableMapper.updateTableParentKey(map);
		}
		return actExTableColsMapper.saveActExTableCols(actExTableModel);
	}

	@Override
	public int deleteActExTableCols(ActExTableColsModel actExTableModel) {
		return actExTableColsMapper.deleteActExTableCols(actExTableModel);
	}

	@Override
	public int updateActExTableCols(OaActExTableColsModel actExTableModel) {
		return actExTableColsMapper.updateActExTableCols(actExTableModel);
	}
	@Override
	public List<OaActExTableColsModel> queryOaActExTableCols(OaActExTableColsModel actExTableModel) {
		return actExTableColsMapper.queryActExTableCols(actExTableModel);
	}

	@Override
	public Map<String, Object> getTablePkAndFks(Map<String, Object> map) {
		if(map == null){
			return null;
		}
		//获取主表对象
		ActExTableColsModel mainModel = (ActExTableColsModel)map.get(Contants.FORM_KEY_MAIN);
		if(mainModel != null && mainModel.getTableKey() != null && !"".equals(mainModel.getTableKey())){
			String keys [] = {mainModel.getTableKey()};
			mainModel.setCheckeds(keys);
			//查询主表的主键
			List<ActExTableColsModel> mainList = actExTableColsMapper.queryTablePKAndFk(mainModel);
			if(mainList!=null&&mainList.size()>0){
				mainModel = mainList.get(0);
				map.put(Contants.FORM_KEY_MAIN, mainModel);
			}
		}
		//获取子表信息
		List<ActExTableColsModel> subs = (List<ActExTableColsModel>)map.get(Contants.FORM_KEY_SUBS);
		if(subs!=null&&subs.size()>0){
			String tableKeys [] = new String [subs.size()];
			for (int i = 0; i < subs.size(); i++) {
				tableKeys[i] = subs.get(i).getTableKey();
			}
			ActExTableColsModel submodel = new ActExTableColsModel();
			submodel.setCheckeds(tableKeys);
			subs = actExTableColsMapper.queryTablePKAndFk(submodel);
			map.put(Contants.FORM_KEY_SUBS, subs);
		}
		return map;
	}
	
}
